package com.itheima.springboottilasmanager.controller;

import com.itheima.springboottilasmanager.pojo.domain.Emp;
import com.itheima.springboottilasmanager.pojo.domain.PageResult;
import com.itheima.springboottilasmanager.pojo.vo.QueryParam;
import com.itheima.springboottilasmanager.pojo.domain.Result;
import com.itheima.springboottilasmanager.survice.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {

@Autowired
private EmpService empService;

@GetMapping
public Result page ( QueryParam queryParam) {
log.info("参数{}", queryParam);

//2.调用service查询数据
//    PageResult<Emp> pageResult = empService.page(queryParam);
    PageResult<Emp> pageResult = empService.page(queryParam);
//3.封装冰返回结果
return Result.success(pageResult);
}
@PostMapping
public Result save(@RequestBody Emp emp){
empService.saveEmp(emp);
return Result.success();
}
@DeleteMapping
    public Result delete(@RequestParam List<Integer> ids )
{
    log.info("批量删除员工{}",ids);
   empService.deleteEmp(ids);

    return Result.success();
}
@GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
    Emp emp =  empService.findById(id);
    return Result.success(emp);
}
@PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("更新员工{}",emp);
        empService.updateEmp(emp);
        return Result.success();
    }
    @GetMapping("/list")
    public Result selectall(){
        log.info("查询所有员工");
        List<Emp> list = empService.selectall();
        return Result.success(list);
    }
}
